/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.swing.dings.tester;

import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

import com.fricano.java42.base.components.helper.Helper_Date;
import com.fricano.java42.base.components.helper.Helper_Image;
import com.fricano.java42.base.components.helper.Helper_System;
import com.fricano.java42.base.components.utility.impl.Utility_Swing;
import com.fricano.java42.lang.System;
import com.fricano.java42.swing.dings.J42SwingDingManager_Recorder;
import com.fricano.java42.swing.dings.J42SwingDingManager_Recorder.RecorderCallback;
import com.fricano.java42.swing.dings.J42SwingDingManager_Recorder.RecorderSettings;
import com.fricano.java42.swing.dings.J42SwingDing_Factory;

final public class J42SwingDing_ScreenRecorderTest_02 {

    private static J42SwingDing_Factory         dingFactory;
    private static J42SwingDingManager_Recorder dingRecorder;
    private static Map                          map = new HashMap();

    static public void main(final Map mapIn, final String[] args) throws Exception {

        map = mapIn;
        main(args);

    }

    static public void main(final String[] args) throws Exception {

        Utility_Swing.setLookAndFeel(Utility_Swing.LookAndFeel.CROSSPLATFORM);

        // --------------------------------
        // Create a SwingDing factory
        // --------------------------------
        dingFactory = new J42SwingDing_Factory(null);

        // --------------------------------
        // Create a screen recorder
        // --------------------------------
        final RecorderCallback recorderCallback = createRecorderCallbackHandler();
        dingRecorder = dingFactory.create_ScreenRecorder(new RecorderSettings(), recorderCallback);

        dingRecorder.startInteractiveMultiFrameRecorderGUI(map);

        Helper_System.createDevelopmentConsole(dingRecorder.getWindow());

    }

    private static RecorderCallback createRecorderCallbackHandler() {

        return new RecorderCallback() {

            @Override
            public void started() {
            }

            @Override
            public final boolean imageFilter(final BufferedImage bufferedImage) {
                return true;
            }

            @Override
            public void stopped(final RecorderSettings recorderSettings, final ImageIcon imageIcon) {
                final String timeStamp = Helper_Date.getInstance().getYYYYMMDDHHMMSS();
                final File file = new File("ScreenCapture" + timeStamp + ".gif");
                final JEditorPane content = new JEditorPane();
                content.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createEmptyBorder(5, 10, 10, 10)));
                content.setContentType("text/html");
                content.setEditable(false);
                content.setBackground(null);
                final Point location = dingRecorder.getWindow().getLocation();
                if (file.exists()) {
                    Toolkit.getDefaultToolkit().beep();
                    dingRecorder.getWindow().setLocation(-9999, -9999);
                    final String contentMessage = "" + "<center>" + "<b>" + "</b>" + "Do you want to overwrite this file?<br>" + file.getAbsolutePath();
                    content.setText("<html>" + contentMessage + "</html>");
                    final int x = JOptionPane.showConfirmDialog(null, content, "Confirm File Replace", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (x != 0) {
                        dingRecorder.getWindow().setLocation(location);
                        return;
                    }
                }
                Helper_Image.saveAnimatedGif(recorderSettings.gifImageInfo, file);
                final long fileSize = file.length();
                System.out.println(imageIcon);
                System.out.println(file.getName() + " (" + fileSize + " bytes)");
                final String contentMessage = "" + "<center>" + "<b>" + "</b>" + "Animated GIF saved<br>" + file.getAbsolutePath();
                content.setText("<html>" + contentMessage + "</html>");

                final JOptionPane op = new JOptionPane(content, JOptionPane.INFORMATION_MESSAGE);
                final JDialog d = op.createDialog(null, "Anamated GIF Saved");
                d.setModal(true);
                d.setAlwaysOnTop(true);
                d.setVisible(true);
                d.dispose();

                dingRecorder.getWindow().setLocation(location);
            }
        };
    }
}
